package com.ruoyi.web.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.DiseaseOpt;
import com.ruoyi.web.domain.OldDrugDisease;
import com.ruoyi.web.service.IOldDrugDiseaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.domain.OldDrugSpecification;
import com.ruoyi.web.mapper.OldDrugMapper;
import com.ruoyi.web.domain.OldDrug;
import com.ruoyi.web.service.IOldDrugService;
import com.ruoyi.common.core.text.Convert;

/**
 * oldDrugService业务层处理
 * 
 * @author drx
 * @date 2024-03-01
 */
@Service
public class OldDrugServiceImpl implements IOldDrugService 
{
    @Autowired
    private OldDrugMapper oldDrugMapper;
    @Autowired
    private IOldDrugDiseaseService oldDrugDiseaseService;

    /**
     * 查询oldDrug
     * 
     * @param id oldDrug主键
     * @return oldDrug
     */
    @Override
    public OldDrug selectOldDrugById(Long id)
    {
        OldDrugDisease oldDrugDisease = new OldDrugDisease();
        oldDrugDisease.setDrugId(id);
        List<OldDrugDisease> list = oldDrugDiseaseService.selectOldDrugDiseaseList(oldDrugDisease);
        OldDrug oldDrug = oldDrugMapper.selectOldDrugById(id);
        oldDrug.setDiseaseList(list);
        return oldDrug;
    }

    /**
     * 前端查询详情用
     *
     * @param id oldDrug主键
     * @return oldDrug
     */
    @Override
    public AjaxResult getOldDrugById(Long id)
    {
        AjaxResult ajaxResult=new AjaxResult();
        try{
            OldDrug oldDrug = oldDrugMapper.selectOldDrugById(id);
            if(null != oldDrug){
                //状态为未启用时，返回空对象
                if("0".equals(oldDrug.getState())){
                    ajaxResult.put("data",null);
                    ajaxResult.put("code","1");
                    ajaxResult.put("msg","查询成功！");
                    return  ajaxResult;
                }
                OldDrugDisease oldDrugDisease = new OldDrugDisease();
                oldDrugDisease.setDrugId(id);
                List<OldDrugDisease> list = oldDrugDiseaseService.selectOldDrugDiseaseList(oldDrugDisease);
                oldDrug.setDiseaseList(list);
            }
            ajaxResult.put("data",oldDrug);
            ajaxResult.put("code","1");
            ajaxResult.put("msg","查询成功！");
        } catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("code","500");
            ajaxResult.put("msg","获取药品详情失败，请联系管理员！");
        }
        return ajaxResult;
    }

    /**
     * 查询oldDrug列表
     * 
     * @param oldDrug oldDrug
     * @return oldDrug
     */
    @Override
    public List<OldDrug> selectOldDrugList(OldDrug oldDrug)
    {
        return oldDrugMapper.selectOldDrugList(oldDrug);
    }

    /**
     * 新增oldDrug
     * 
     * @param oldDrug oldDrug
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOldDrug(OldDrug oldDrug)
    {
        oldDrug.setCreateTime(DateUtils.getNowDate());
        oldDrug.setCreateBy(ShiroUtils.getSysUser().getLoginName());
        oldDrug.setCreateName(ShiroUtils.getSysUser().getUserName());
        oldDrug.setUpdateTime(DateUtils.getNowDate());
        oldDrug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        oldDrug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        int rows = oldDrugMapper.insertOldDrug(oldDrug);
        List<OldDrugDisease> list = oldDrug.getDiseaseList();
        if(list.size()>0){
            list.forEach(item->{
                OldDrugDisease oldDrugDisease = new OldDrugDisease();
                oldDrugDisease.setDiseaseId(item.getDiseaseId());
                oldDrugDisease.setPid(item.getPid());
                oldDrugDisease.setDrugId(oldDrug.getId());
                oldDrugDiseaseService.insertOldDrugDisease(oldDrugDisease);
            });

        }
        insertOldDrugSpecification(oldDrug);
        return rows;
    }

    /**
     * 修改oldDrug
     * 
     * @param oldDrug oldDrug
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOldDrug(OldDrug oldDrug)
    {
        oldDrug.setUpdateTime(DateUtils.getNowDate());
        oldDrug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        oldDrug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        List<OldDrugDisease> list = oldDrug.getDiseaseList();
        //根据药品id删除疾病
        oldDrugDiseaseService.deleteOldDrugDiseaseByDrugId(oldDrug.getId());
        list.forEach(item->{
            OldDrugDisease oldDrugDisease = new OldDrugDisease();
            oldDrugDisease.setDiseaseId(item.getDiseaseId());
            oldDrugDisease.setPid(item.getPid());
            oldDrugDisease.setDrugId(oldDrug.getId());
            oldDrugDiseaseService.insertOldDrugDisease(oldDrugDisease);
        });
        oldDrugMapper.deleteOldDrugSpecificationByDrugId(oldDrug.getId(),ShiroUtils.getSysUser().getLoginName());
        insertOldDrugSpecification(oldDrug);
        return oldDrugMapper.updateOldDrug(oldDrug);
    }

    /**
     * 修改oldDrug
     *
     * @param oldDrug oldDrug
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOldDrugState(OldDrug oldDrug)
    {
        oldDrug.setUpdateTime(DateUtils.getNowDate());
        oldDrug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        oldDrug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return oldDrugMapper.updateOldDrug(oldDrug);
    }


    /**
     * 批量删除oldDrug
     * 
     * @param ids 需要删除的oldDrug主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOldDrugByIds(String ids)
    {
        oldDrugMapper.deleteOldDrugSpecificationByDrugIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
        return oldDrugMapper.deleteOldDrugByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除oldDrug信息
     * 
     * @param id oldDrug主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOldDrugById(Long id)
    {
        oldDrugMapper.deleteOldDrugSpecificationByDrugId(id,ShiroUtils.getSysUser().getLoginName());
        return oldDrugMapper.deleteOldDrugById(id,ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 新增旧药品规格信息
     * 
     * @param oldDrug oldDrug对象
     */
    public void insertOldDrugSpecification(OldDrug oldDrug)
    {
        List<OldDrugSpecification> oldDrugSpecificationList = oldDrug.getOldDrugSpecificationList();
        Long id = oldDrug.getId();
        if (StringUtils.isNotNull(oldDrugSpecificationList))
        {
            List<OldDrugSpecification> list = new ArrayList<OldDrugSpecification>();
            for (OldDrugSpecification oldDrugSpecification : oldDrugSpecificationList)
            {
                oldDrugSpecification.setDrugId(id);
                oldDrugSpecification.setCreateTime(DateUtils.getNowDate());
                oldDrugSpecification.setCreateName(ShiroUtils.getSysUser().getLoginName());
                oldDrugSpecification.setDelFlag("0");
                list.add(oldDrugSpecification);
            }
            if (list.size() > 0)
            {
                oldDrugMapper.batchOldDrugSpecification(list);
            }
        }
    }
}
